{extend name="default/base" /}

{block name="title"}{$title}{/block}
{block name="keywords"}{$keywords}{/block}
{block name="description"}{$description}{/block}

{block name="css"}{/block}

{block name="content"}

<div class="row">
    <div class="col-sm-8">
          <div class="box box-solid">
            <div class="box-header with-border">
                <h3 class="text-center"><a href="{$article_url}">{$title}</a></h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <div class="pad">
                    <form method="post" id="form" action="">
                        <div class="form-group">
                          <textarea class="form-control" name="content" rows="3" placeholder="文明发言,理性评论"></textarea>
                        </div>
                        <div class="form-group">
                            <input type="hidden" name="article_id" value="{$id}">
                            <button type="button" id="submit" class="btn btn-primary btn-flat pull-right">提交</button>
                        </div>
                    </form>
                </div>
            </div>
            <!-- /.box-body -->
            <div class="box-footer text-center">
            </div>

            <div class="box box-widget">
                <div class="box-header with-border">
                    <i class="fa fa-comments-o"></i>
                    <h3 class="box-title">评论</h3>
                    <div class="box-tools">
                      <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                    </div>
                    <!-- /.box-tools -->
                </div>
                <!-- /.box-header -->
                <div class="box-body no-padding">
                    <div class="box-comments pad">
                        {volist name="list" id="vo"}
                        <div class="box-comment">
                            <!-- User image -->
                            <img class="img-circle img-sm" src="{$vo.from_avatar}">
                            <div class="comment-text">
                                <span class="username">
                                    {$vo.from_username}
                                    <span class="text-muted pull-right">{$vo.create_at|format_date}</span>
                                </span><!-- /.username -->
                                {$vo.content}
                                <div class="text-muted small">
                                    <a href="javascript:;" onclick="like({$vo.id})"><i class="fa fa-thumbs-o-up"></i> 喜欢(<span id="like_{$vo.id}">{$vo.like}</span>)</a> 
                                    <a href="javascript:;" onclick="reply({$vo.id},{$vo.from_uid})" style="margin-left:10px;"><i class="fa fa-share"></i> 回复({$vo.child_sum})</a>
                                </div>
                                {notempty name="vo.child"}
                                <div class="pad">
                                    <hr>
                                    {volist name="vo.child" id="ch"}
                                    <div class="box-comment">
                                        <!-- User image -->
                                        <img class="img-circle img-sm" src="{$ch.from_avatar}" alt="User Image">
                                        <div class="comment-text">
                                            <span class="username">
                                           {$ch.from_username}
                                            <span class="text-muted pull-right">{$ch.create_at|format_date}</span>
                                            </span><!-- /.username -->
                                            {$ch.content}
                                        </div>
                                        <!-- /.comment-text -->
                                    </div>
                                    {/volist}
                                </div>
                                {/notempty}
                                <div id="comment_id_{$vo.id}" class="reply" style="display:none;">
                                    <form method="post" id="form_{$vo.id}">
                                        <div class="form-group">
                                          <textarea class="form-control" name="content" rows="3" placeholder="文明发言,理性评论"></textarea>
                                        </div>
                                        <div class="form-group">
                                            <input type="hidden" name="article_id" value="{$id}">
                                            <input type="hidden" name="parent_id" value="{$vo.id}">
                                            <input type="hidden" name="to_uid" value="{$vo.from_uid}">
                                            <button type="button" id="submit_{$vo.id}" class="btn btn-primary btn-flat pull-right">提交</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-comment -->
                        {/volist}
                    </div>
                </div>
                <!-- /.box-body -->

                <div class="box-footer">
                {$page}
                </div>
                <!-- /.box-footer -->
            </div>

          </div>
          <!-- /. box -->
    </div>
    
    <div class="col-sm-4 hidden-xs">
        <div class="box box-solid">
            <div class="box-header with-border">
              <i class="fa fa-fire text-light-blue"></i>
              <h3 class="box-title">热点</h3>
            </div>
            <div class="box-body no-padding">
              <ul class="nav nav-pills nav-stacked small">
                {article:position name='hot' hot='1' order='hot desc' limit='5'}
                <li><a href="{$hot.article_url}">{$hot.title}</a></li>
                {/article:position}
              </ul>
            </div>
            <!-- /.box-body -->
        </div>

        <div class="box box-solid">
            <div class="box-header with-border">
                <i class="fa fa-tags text-light-blue"></i>
                <h3 class="box-title">标签</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <ul class="nav nav-pills">
                    {tag:list name='vo' order='num desc' limit='10'}
                    <li role="presentation"><a href="{$vo.tag_url}">{$vo.title}</a></li>
                    {/tag:list}
                </ul>
            </div>
            <!-- /.box-body -->
        </div>

    </div>

</div>

{/block}

{block name="js"}
<script type="text/javascript">
$(document).ready(function(){
  $("#submit").click(function(){
                $.ajax({
                    url:"{:url('api/comment/add')}",
                    data:$(form).serialize(),
                    type:'post',
                    dataType:'json',
                    success:function(xhr){
                        if(xhr.code == 200){
                            swal({
                                title: xhr.msg,
                                text: "",
                                type: "success",
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                            });
                        } else{
                            swal({
                                title: xhr.msg,
                                text: "",
                                type: "error",
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                            });
                        }
                    }
                });
  });
});
</script>

<script type="text/javascript">
function reply(comment_id,uid){
    var a = $("#comment_id_"+comment_id);
    var display = a.css('display');
    // 如果当前显示状态 再次点击隐藏
    if (display == "block") {
        a.css("display", "none");
    } else {
        $(".reply").css("display", "none");
        a.css("display", "block");
    }
    
    $("#submit_"+comment_id).click(function(){
        $.ajax({
            url:"{:url('api/comment/add')}",
            data:$("#form_"+comment_id).serialize(),
            type:'post',
            dataType:'json',
            success:function(xhr){
                if(xhr.code == 200){
                    swal({
                        title: xhr.msg,
                        text: "",
                        type: "success",
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                    });
                } else{
                    swal({
                        title: xhr.msg,
                        text: "",
                        type: "error",
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                    });
                }
            }
        });
    });

}
</script>

<script type="text/javascript">
function like(id){
    $.ajax({
        url:"{:url('api/comment/like')}",
        data:{id:id},
        type:'post',
        dataType:'json',
        success:function(xhr){
            if(xhr.code == 200){
                swal({
                    title: xhr.msg,
                    text: "",
                    type: "success",
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                });
                
            } else{
                swal({
                    title: xhr.msg,
                    text: "",
                    type: "error",
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                });
            }
        }
    });
}
</script>
{/block}